package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.util.StringUtils;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQueryVo;
import com.atguigu.eduservice.entity.vo.DetailedCourseInfoVo;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-07-12
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    EduCourseDescriptionService eduCourseDescriptionService;
    @Autowired
    public void setEduCourseDescriptionService(EduCourseDescriptionService eduCourseDescriptionService) {
        this.eduCourseDescriptionService = eduCourseDescriptionService;
    }

    EduVideoService eduVideoService;
    @Autowired
    public void setEduVideoService(EduVideoService eduVideoService) {
        this.eduVideoService = eduVideoService;
    }

    EduChapterService eduChapterService;
    @Autowired
    public void setEduChapterService(EduChapterService eduChapterService) {
        this.eduChapterService = eduChapterService;
    }

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse course = new EduCourse();
        // 属性传递
        BeanUtils.copyProperties(courseInfoVo,course);
        course.setIsDeleted(0);
        int inset = baseMapper.insert(course);
        if(inset <= 0){
            throw new GuliException(20001,"添加信息失败");
        }
        String  courseDescriptionId = course.getId();
        // 保存课程介绍
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseDescriptionId);
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescriptionService.save(eduCourseDescription);
        return courseDescriptionId;
    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {
        // 添加课程信息
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        // 属性传递
        BeanUtils.copyProperties(eduCourse,courseInfoVo);
        // 添加描述信息
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(courseId);
        if(eduCourseDescription == null){
            // 如果没有描述信息就返回空值
            courseInfoVo.setDescription("");
        }
        else{
            courseInfoVo.setDescription(eduCourseDescription.getDescription());
        }

        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        EduCourse eduCourse = new EduCourse();
        // 属性传递
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        // 更新课程信息表
        int updateCount = baseMapper.updateById(eduCourse);
        if(updateCount <= 0){
            throw new GuliException(20001,"修改信息课程失败!");
        }
        // 更新课程简介表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        // 属性传递
        BeanUtils.copyProperties(courseInfoVo,eduCourseDescription);
        eduCourseDescriptionService.updateById(eduCourseDescription);
    }

    @Override
    public CoursePublishVo getPublishCourseInfo(String courseId) {
        return baseMapper.getPublishCourseInfo(courseId);
    }

    @Override
    public boolean removeCourse(String courseId) {
        //TODO:根据课程id来删除云端图片

        // 根据课程id来删除小节与云端视频
        eduVideoService.removeByCourseId(courseId);
        // 根据课程id来删除章节
        eduChapterService.removeByCourseId(courseId);
        // 根据课程id来删除课程描述
        eduCourseDescriptionService.removeById(courseId);
        // 根据课程id来删除课程本身
        if(baseMapper.deleteById(courseId) > 0){
            return true;
        }
        return false;
    }

    @Cacheable(value = "hotCourse", key = "'selectIndexList'")
    @Override
    public List<EduCourse> getHotCourse() {
        // 查询前八条热门课程
        QueryWrapper<EduCourse> courseQueryWrapper = new QueryWrapper<>();
        courseQueryWrapper.orderByDesc("id");
        courseQueryWrapper.last("limit 8");
        List<EduCourse> courses = baseMapper.selectList(courseQueryWrapper);
        return courses;
    }

    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageCourse, CourseQueryVo courseQueryVo) {
        // 创建条件构造器
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseQueryVo.getSubjectParentId())) {
            queryWrapper.eq("subject_parent_id", courseQueryVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseQueryVo.getSubjectId())) {
            queryWrapper.eq("subject_id", courseQueryVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseQueryVo.getBuyCountSort())) {
            queryWrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseQueryVo.getGmtCreateSort())) {
            queryWrapper.orderByDesc("gmt_create");
        }
        if (!StringUtils.isEmpty(courseQueryVo.getPriceSort())) {
            queryWrapper.orderByDesc("price");
        }

        // 查询
        baseMapper.selectPage(pageCourse, queryWrapper);

        // 封装信息进map
        List<EduCourse> records = pageCourse.getRecords();
        long current = pageCourse.getCurrent();
        long pages = pageCourse.getPages();
        long size = pageCourse.getSize();
        long total = pageCourse.getTotal();
        boolean hasNext = pageCourse.hasNext();
        boolean hasPrevious = pageCourse.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        // 返回封装信息
        return map;
    }

    @Override
    public DetailedCourseInfoVo selectDetailedCourseInfo(String courseId) {
        return baseMapper.getDetailedCourseInfo(courseId);
    }
}
